#!/bin/bash

# Matrix Framework 系统启动器 (Linux/macOS)

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

print_header() {
    echo "==============================================="
    echo "       Matrix Framework 系统启动器"
    echo "==============================================="
    echo
}

# 检查Docker
check_docker() {
    print_message $BLUE "[1/5] 检查Docker环境..."

    if ! command -v docker &> /dev/null; then
        print_message $RED "❌ Docker未安装或未运行"
        exit 1
    fi

    if ! docker info &> /dev/null; then
        print_message $RED "❌ Docker未运行，请先启动Docker"
        exit 1
    fi

    print_message $GREEN "✅ Docker环境正常"
}

# 检查配置文件
check_config() {
    print_message $BLUE "[2/5] 检查配置文件..."

    if [ ! -f "src/docker/docker-compose.microservices.yml" ]; then
        print_message $RED "❌ 找不到docker-compose.microservices.yml文件"
        exit 1
    fi

    print_message $GREEN "✅ 配置文件存在"
}

# 清理现有容器
cleanup_containers() {
    print_message $BLUE "[3/5] 清理现有容器..."
    docker-compose -f src/docker/docker-compose.microservices.yml down -v 2>/dev/null || true
    print_message $GREEN "✅ 清理完成"
}

# 启动微服务
start_services() {
    print_message $BLUE "[4/5] 启动微服务..."
    print_message $YELLOW "    正在启动数据库、缓存、后端服务..."

    docker-compose -f src/docker/docker-compose.microservices.yml up -d --build

    if [ $? -ne 0 ]; then
        print_message $RED "❌ 微服务启动失败"
        exit 1
    fi

    print_message $GREEN "✅ 微服务启动成功"
}

# 等待服务启动
wait_for_services() {
    print_message $BLUE "[5/5] 等待服务启动..."
    sleep 10
}

# 检查服务状态
check_services_status() {
    echo
    print_message $BLUE "📊 服务状态检查："
    docker-compose -f src/docker/docker-compose.microservices.yml ps
}

# 显示启动完成信息
show_completion_info() {
    echo
    print_header
    print_message $GREEN "           系统启动完成！"
    print_header
    echo

    print_message $BLUE "🌐 访问地址："
    echo "   前端应用:      http://localhost:3000/"
    echo "   API网关:       http://localhost:8080/"
    echo "   服务注册中心:   http://localhost:5000/"
    echo

    print_message $BLUE "📚 API文档地址："
    echo "   租户管理API:    http://localhost:5001/swagger (服务启动后)"
    echo "   用户系统API:    http://localhost:5002/swagger (服务启动后)"
    echo

    print_message $BLUE "💡 系统管理："
    echo "   查看日志: docker-compose -f src/docker/docker-compose.microservices.yml logs"
    echo "   停止系统: docker-compose -f src/docker/docker-compose.microservices.yml down"
    echo

    print_message $BLUE "🔍 健康检查："
    echo "   检查服务: sleep 5 && docker-compose -f src/docker/docker-compose.microservices.yml ps"
    echo

    print_message $BLUE "📝 接下来请启动前端应用："
    echo "   cd src/web/frontend"
    echo "   npm run dev"
    echo
}

# 询问是否启动前端
ask_start_frontend() {
    echo -n "是否现在启动前端应用？(y/n): "
    read -r response

    if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
        echo
        print_message $BLUE "🚀 启动前端应用..."
        cd src/web/frontend

        # 检查node_modules是否存在
        if [ ! -d "node_modules" ]; then
            print_message $YELLOW "📦 安装前端依赖..."
            npm install
        fi

        print_message $BLUE "🎯 启动前端开发服务器..."
        npm run dev &

        print_message $GREEN "✅ 前端应用正在启动，请等待几秒后访问 http://localhost:3000/"
    fi
}

# 主函数
main() {
    print_header

    check_docker
    check_config
    cleanup_containers
    start_services
    wait_for_services
    check_services_status
    show_completion_info
    ask_start_frontend

    echo
    print_message $GREEN "🎉 系统启动完成！如需帮助，请查看 STARTUP_GUIDE.md"
    echo
}

# 执行主函数
main "$@"